﻿
// CMainDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "CApp.h"
#include "CMainDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMainDlg 对话框



CMainDlg::CMainDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_MAIN_DLG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}



void CMainDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CMainDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ADD, &CMainDlg::OnBnClickedAdd)
	ON_BN_CLICKED(IDC_DEL, &CMainDlg::OnBnClickedDel)
	ON_BN_CLICKED(IDC_MOD, &CMainDlg::OnBnClickedMod)
	//ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST, &CMainDlg::OnLvnItemchangedList)
	ON_BN_CLICKED(IDC_SAVE, &CMainDlg::OnBnClickedSave)
END_MESSAGE_MAP()


// CMainDlg 消息处理程序

BOOL CMainDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 初始化列表
	auto pList = (CListCtrl*)GetDlgItem(IDC_LIST);
	pList->InsertColumn(1, L"工号", LVCFMT_CENTER, 140);
	pList->InsertColumn(1, L"姓名", LVCFMT_CENTER, 150);
	pList->InsertColumn(2, L"工资", LVCFMT_CENTER, 160);
	pList->InsertColumn(3, L"入职日期", LVCFMT_CENTER, 180);
	pList->SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);

	Load();
	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CMainDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CMainDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


int CMainDlg::CheckNum(const CString& str)
{
	auto pList = (CListCtrl*)GetDlgItem(IDC_LIST);
	int nCount = pList->GetItemCount();
	while (nCount--)
	{
		if (pList->GetItemText(nCount, 0) == str)
			return nCount;
	}
	return -1;
}

void CMainDlg::Load()
{
	// 打开文件，读文件
	CFile file;
	if (!file.Open(L"./Data.txt", CFile::modeRead))
	{
		perror("fopen");
		return;
	}
	auto pList = (CListCtrl*)GetDlgItem(IDC_LIST);
	pList->DeleteAllItems();	// 清空一下
	sData d;
	CString s;
	while (file.Read(&d, sizeof(d)) == sizeof(d))
	{
		int nCount = pList->GetItemCount();
		s.Format(L"%d", d.id);
		pList->InsertItem(nCount, s);
		pList->SetItemText(nCount, 1, d.name);
		s.Format(L"%g", d.salary);
		pList->SetItemText(nCount, 2, s);
		pList->SetItemText(nCount, 3, d.date);
	}

	file.Close();
}


void CMainDlg::OnBnClickedAdd()
{
	auto pList = (CListCtrl*)GetDlgItem(IDC_LIST);
	CString str;
	this->GetDlgItemText(IDC_NUMB, str);
	
	int idx = this->CheckNum(str);
	if (idx >= 0)
	{
		str = L"工号: " + str + L"\r\n";
		str += (L"姓名: " + pList->GetItemText(idx, 1) + L"\r\n");
		str += (L"工资: " + pList->GetItemText(idx, 2) + L"\r\n");
		str += (L"入职时间: " + pList->GetItemText(idx, 3) + L"\r\n");

		MessageBox(L"你输入的工号: " + pList->GetItemText(idx, 0) + "已经存在!\r\n" + str, L"提示", MB_OK | MB_ICONINFORMATION);
		return;
	}
	
	int nCount = pList->GetItemCount();
	pList->InsertItem(nCount, str);
	GetDlgItemText(IDC_NAME, str);
	pList->SetItemText(nCount, 1, str);
	GetDlgItemText(IDC_SALARY, str);
	pList->SetItemText(nCount, 2, str);
	GetDlgItemText(IDC_DATE, str);
	pList->SetItemText(nCount, 3, str);
	OnBnClickedSave();	// 添加完自动保存
}


void CMainDlg::OnBnClickedDel()
{
}


void CMainDlg::OnBnClickedMod()
{
}


void CMainDlg::OnBnClickedSave()
{
	// 保存到当前目录文件中
	CFile file;
	if (!file.Open(L"./Data.txt", CFile::modeWrite | CFile::modeCreate))
	{
		MessageBox(L"保存失败", L"提示", MB_OK | MB_ICONINFORMATION);
		return;
	}

	auto pList = (CListCtrl*)GetDlgItem(IDC_LIST);
	int n = pList->GetItemCount();
	int i = -1;
	sData d;
	while (++i < n)
	{
		d.id = _tstoi(pList->GetItemText(i, 0));
		_tcscpy(d.name, pList->GetItemText(i, 1));
		d.salary = static_cast<float>(_tstof(pList->GetItemText(i, 2)));
		_tcscpy(d.date, pList->GetItemText(i, 3));
		file.Write(&d, sizeof(d));
	}
	
	file.Close();
}
